<?
    class Questions
    { 
	  
	var $db;
	var $tbl_questions;
	var $tbl_answers;
	var $tbl_statquestions;
	var $tbl_ips;
	
	function Questions($db,$tbl_questions,$tbl_answers,$tbl_statquestions,$tbl_ips)
 	{
       $this->db = $db;
	   $this->tbl_questions = $tbl_questions;
	   $this->tbl_answers = $tbl_answers;
	   $this->tbl_statquestions = $tbl_statquestions;
	   $this->tbl_ips = $tbl_ips;
	}
	
	function InsertQuestion($post)
	{
	   $q = $this -> db -> MakeQuery($post);
	   $query = "insert into ".$this->tbl_questions." set ".$q;
	   $this -> db -> RunQuery($query);
	}
	
	function SelectQuestion()
	{ 
	   $query = "select * from ".$this->tbl_questions." order by number DESC";
	   $arr = $this -> db -> ArrFromQuery($query);
	   return $arr;
	}	

	function SelectActiveQuestion()
	{
       $query = "select * from ".$this->tbl_questions."  where status='1' order by id DESC";
	   $arr = $this -> db -> ArrFromQuery($query);
	   return $arr;
	}
	
	function SelectOneQuestion($id)
	{
       $query = "select * from ".$this->tbl_questions." where id='".(int)$id."'";
	   $arr = $this -> db -> ArrFromQuery($query);
	   return $arr;
	}
      
    function UpdateQuestion($post,$qid)
	{
	   $q = $this -> db -> MakeQuery($post);
  	   $query = "update ".$this->tbl_questions." set ".$q." where id='".$qid."'";   
       $arr = $this -> db -> ArrFromQuery($query);
	   return $arr;
	}
	
	function DeleteQuestion($id)
	{
	   $query = "delete from ".$this->tbl_questions." where id='$id'";
	   $this -> db -> RunQuery($query);
	   $this -> DeleteIpByQuestion($id);
	   $this -> DeleteStatisticByQuestion($id);
	   $this -> DeleteAnswerByQuestion($id);
	}
	
	function ChangeQuestionStatus($id,$status)
	{
	    if ( $status == 1 )
	       $status = 0;
            elseif ( $status == 0 )
               $status = 1;     
	    $query = "update ".$this->tbl_questions." set status='$status' where id='$id'";
	    $this -> db -> RunQuery($query);
	}
	
	function GetLastQuestionNumber()
	{
	    $arr0 = $this -> SelectQuestion();
		if (count($arr0) > 0)
		{
		   $arr1 = $this -> total -> MakeArrayByKey($arr0,"number");
           $number = max($arr1);
		}
		else $number = 1;
		return $number;
	}
	
	function ChangeQuestionNumber($x,$qid,$nqid,$number,$nnumber)
	{
        $ch_number = $nnumber;
	    $query[0] = "update ".$this->tbl_questions." set number=".$ch_number." where id=".$qid;
		$query[1] = "update ".$this->tbl_questions." set number=".$number." where id=".$nqid;
        for ($i=0;$i<2;$i++)
            $this -> db -> RunQuery($query[$i]);
	}
	
	function InsertAnswer($post,$qid)
	{
	   $q = $this -> db -> MakeQuery($post);
	   $query = "insert into ".$this->tbl_answers." set ".$q.", qid='".(int)$qid."'";
	   $id = $this -> db -> GetLastId($query);
	   return $id;
	}
	
	function SelectAnswer()
	{
       $query = "select * from ".$this->tbl_answers." order by number DESC";
	   $arr = $this -> db -> ArrFromQuery($query);
	   return $arr;
      }	

	function SelectAnswerByQuestion($qid)
	{
       $query = "select * from ".$this->tbl_answers." where qid='".(int)$qid."' order by number DESC";
	   $arr = $this -> db -> ArrFromQuery($query);
	   return $arr;
	}
	
	function SelectOneAnswer($id)
	{
       $query = "select * from ".$this->tbl_answers." where id='".(int)$id."'";
	   $arr = $this -> db -> ArrFromQuery($query);
	   return $arr;
	}
      
    function UpdateAnswer($post,$aid)
	{
	   $q = $this -> db -> MakeQuery($post);
  	   $query = "update ".$this->tbl_answers." set ".$q." where id='".(int)$aid."'";   
       $arr = $this -> db -> ArrFromQuery($query);
	   return $arr;
	}
	
	function DeleteAnswer($id)
	{
	   $query = "delete from ".$this->tbl_answers." where id='$id'";
	   $this -> db -> RunQuery($query);
	   $this -> DeleteStatistic($id);
	}

	function GetLastAnswerNumber($qid)
	{
	    $arr0 = $this -> SelectAnswerByQuestion((int)$qid);
		if (count($arr0) > 0)
		{
		   $arr1 = $this -> total -> MakeArrayByKey($arr0,"number");
           $number = max($arr1);
	    }
		else $number = 1;
		return $number;
	}

	function ChangeAnswerNumber($x,$qid,$nqid,$number,$nnumber)
	{
	    $ch_number = $nnumber;
	    $query[0] = "update ".$this->tbl_answers." set number=".$ch_number." where id=".$qid;
		$query[1] = "update ".$this->tbl_answers." set number=".$number." where id=".$nqid;
        for ($i=0;$i<2;$i++)
            $this -> db -> RunQuery($query[$i]);
	}
	
	function DeleteAnswerByQuestion($qid)
	{
	   $query = "delete from ".$this->tbl_answers." where qid='$qid'";
	   $this -> db -> RunQuery($query);
	}
	
 	function InsertStatistic($qid,$aid)
	{
	   $query = "insert into ".$this->tbl_statquestions." set qid='$qid', aid='$aid', count='0'";
       $this->db->RunQuery($query);
	}
	
	function SelectStatistic($qid)
	{
       $query = "select * from ".$this->tbl_statquestions." where qid='".(int)$qid."' order by aid DESC";
	   $arr = $this -> db -> ArrFromQuery($query);
	   return $arr;
    }	
	
	function SelectOneStatistic($aid)
	{
       $query = "select * from ".$this->tbl_statquestions." where aid='".(int)$aid."'";
	   $arr = $this -> db -> ArrFromQuery($query);
	   return $arr;
	}
                              
    function UpdateStatistic($aid)
	{
	    $query = "update ".$this->tbl_statquestions." set count=count+1 where aid='".$aid."'";
	    $this -> db -> RunQuery($query);
	}
	
	function UpdateForumvoteStatistic($aid)
	{
	    $query = "update ".$this->tbl_statquestions." set score=score+1, count=count+1 where aid='".$aid."'";
	    $this -> db -> RunQuery($query);
	}
	
	function DeleteStatistic($aid)
	{
	    $query = "delete from ".$this->tbl_statquestions." where aid='$aid'";
	    $this -> db -> RunQuery($query);
	}
	
	function DeleteStatisticByQuestion($qid)
	{
	    $query = "delete from ".$this->tbl_statquestions." where qid='$qid'";
	    $this -> db -> RunQuery($query);
	} 
	
	function DeleteIpByQuestion($qid)
	{
	   $query = "delete from ".$this->tbl_ips." where qid='".$qid."'";
	    $this -> db -> RunQuery($query);
	}
	
	function InsertIP($qid,$IP,$user_id)
	{
	    $time = time()+86400;
	    $date = date("Y-m-d H:i",$time);
	    $query = "insert into $this->tbl_ips set qid='$qid', ip='$IP', date='$date', user_id='$user_id'";
	    $this -> db -> RunQuery($query);
	}
   
   	function InsertID($qid,$user_id)
	{
	    $query = "insert into $this->tbl_ips set qid='$qid', user_id='$user_id'";
	    $this -> db -> RunQuery($query);
	}
	
    function CheckIP($qid,$IP,$user_id)
	{ 
		if ($user_id == -1)
	       $query = "select * from ".$this->tbl_ips." where  qid='".(int)$qid."' and ip='$IP'";
        else
	       $query = "select * from ".$this->tbl_ips." where  qid='".(int)$qid."' and user_id='$user_id'";
		$arr = $this -> db -> ArrFromQuery($query); 
	    if (count($arr)>0)
        {
	       if (($arr[0]['date'] <= date("Y-m-d H:i",time())) && ($user_id == -1))
	       {	
		   	      $query = "delete from ".$this->tbl_ips." where qid='".(int)$qid."' and ip='$IP'";
		          $this -> db -> RunQuery($query); 
                  return true; 
	       }
           return false;	
		 }
         else return true;
	}   
	
    function CheckID($qid,$ID)
	{
	    $query = "select * from ".$this->tbl_ips." where  qid='".(int)$qid."' and user_id='$ID'";
        $arr = $this -> db -> ArrFromQuery($query); 
	    if ( count($arr)>0 )
                  return false; 
        else return true;	
 	}   
// 
	function DrawStatistic($stat_ar)
	{
	    $st_count = count($stat_ar);
          $sum = 0;
	    for ( $i=0; $i < $st_count; $i++ )
	        $sum += $stat_ar[$i]->count;
         echo "<tr><td></td><td><img src=\"../../cms/grafic/0.PNG\" width=\"300\" height=\"10\"></td><td>100%</td><td>".$sum."</td></tr>";  
          for ( $i=0; $i< $st_count; $i++)
	    {
        if ( $stat_ar[$i]->count != 0 ) 
           $procent = round(100*($stat_ar[$i]->count)/$sum);
              else $procent = 0;
              $answer = $this -> SelectOneAnswer($stat_ar[$i]['aid']);
              echo "<tr><td>".($answer[0]['text'])."</td><td>";
              echo "<img src=\"../../cms/grafic/".($i+1).".PNG\" width=\"".($procent*3)."\" height=\"10\"></td><td>";  
	          echo $procent."%</td><td>".($stat_ar[$i]['count'])."</td></tr>";
          }   
   }
	
	function MakeStatistic($stat_ar)
	{
	    $stat_res = array();
	    $st_count = count($stat_ar);
        $sum = 0;
	    for ( $i=0; $i < $st_count; $i++ )
    		$sum += $stat_ar[$i]['count'];
        for ( $i=0; $i< $st_count; $i++)
	    {
             if ( $stat_ar[$i]['count'] != 0 ) 
                $procent = round(100*($stat_ar[$i]['count'])/$sum);
             else $procent = 0;
			  $stat_res[$i]['proc'] = $procent*3;
			  $stat_res[$i]['count'] = $stat_ar[$i]['count'];
        }   
		return $stat_res;  
   }
	
	function MakeForumvoteStatistic($stat_ar)
	{
	    $stat_res = array();
	    $st_count = count($stat_ar);
        $sum = 0;
	    for ( $i=0; $i < $st_count; $i++ )
    		$sum += $stat_ar[$i]['score'];
        for ( $i=0; $i< $st_count; $i++)
	    {
             if ( $stat_ar[$i]['score'] != 0 ) 
                $procent = round(100*($stat_ar[$i]['score'])/$sum);
             else $procent = 0;
			  $stat_res[$i]['proc'] = $procent*3;
			  $stat_res[$i]['score'] = $stat_ar[$i]['score'];
        	  $stat_res[$i]['count'] = $stat_ar[$i]['count'];
        }   
		return $stat_res;  
   }
	
	function CleanStatistics($qid)
	{
       $query = "update ".$this->tbl_statquestions." set count='0' where qid='$qid'";
	   $this -> db -> RunQuery($query);
	}
   }
?>